<template>
  <div>
    <a-button @click="switchSrc">switchSrc</a-button>

    <div>
      <div>
        <a-radio-group :value="currentDrawType">
          <a-radio-button
            value="Select"
            @click="currentDrawTypeChange('Select')"
          >
            <a-icon type="edit" />
          </a-radio-button>
          <a-radio-button value="Rect" @click="currentDrawTypeChange('Rect')">
            <a-icon type="border" />
          </a-radio-button>
          <a-radio-button value="Line" @click="currentDrawTypeChange('Line')">
            <a-icon type="line" />
          </a-radio-button>
          <a-radio-button value="Point" @click="currentDrawTypeChange('Point')">
            <a-icon type="ellipsis" />
          </a-radio-button>
          <a-radio-button value="small" @click="$refs.drawpanel.resize()">
            <a-icon type="sync" />
          </a-radio-button>
          <a-radio-button value="small" @click="$refs.drawpanel.undo()">
            <a-icon type="undo" />
          </a-radio-button>
          <a-radio-button value="small" @click="$refs.drawpanel.clear()">
            <a-icon type="delete" />
          </a-radio-button>
        </a-radio-group>
      </div>
      <div>
        <a-row>
          <a-col
          :span="12"
            ><drawpanel
              :data="data"
              @drawn="drawn"
              ref="drawpanel"
              :src="src"
              :drawType="currentDrawType"
            ></drawpanel
          ></a-col>
          <a-col :span="12">
            <a-row v-for="item in dataWithoutLabel" :key="item.id">
              <a-col>{{ item.id }}</a-col>
              <a-col>
                <a-button @click="$refs.drawpanel.removeById(item.id)"
                  >remove</a-button
                >
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </div>
    </div>
  </div>
</template>

<script>
import drawpanel from "./panel/index.vue";
export default {
  components: { drawpanel },
  data() {
    return {
      data: [],
      src: "",
      currentDrawType: "",
      urlIndex: 0,
      imageUrls: [
        "https://www.shijuepi.com/uploads/allimg/200821/1-200R1141258.jpg",
        "https://img.zcool.cn/community/0169ec5ca46bb8a8012141685daea4.jpg@1280w_1l_2o_100sh.jpg",
        "https://www.beihaiting.com/uploads/allimg/150307/10723-15030G20Ab07.jpg",
      ],
    };
  },
  computed: {
    dataWithoutLabel() {
      return this.data.filter((d) => d.drawType != "Label");
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.switchSrc();
    });
  },
  methods: {
    drawn(options) {
      this.data.push({
        id: new Date().getTime(),
        drawType: "Label",
        bindId: options.id,
        text: "label_" + new Date().getTime(),
      });
    },
    switchSrc() {
      this.src = this.imageUrls[this.urlIndex++ % this.imageUrls.length];
    },
    currentDrawTypeChange(type) {
      this.currentDrawType = type;
    },
  },
};
</script>

<style></style>
